package com.lianboke.link.ad.updateUtils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.text.TextUtils;
import android.widget.TextView;
import android.widget.Toast;

import com.lianboke.link.ad.VolleyUtils.HttpCallBack;
import com.lianboke.link.ad.VolleyUtils.HttpUtil;
import com.lianboke.link.ad.app.MyApplication;
import com.lianboke.link.ad.beans.UpdateAppInfoModel;
import com.lianboke.link.ad.common.CommonParams;
import com.lianboke.link.ad.utils.DeviceUtils;
import com.lianboke.link.ad.utils.KLog;
import com.lianboke.link.ad.utils.ToastUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import static android.content.Context.BIND_AUTO_CREATE;

/**
 *  单独的下载管理
 * Created by admin on 2017/12/12.
 */

public class DownloadAppManager{
    public static final String LOG_TAG = "check_down";
    private static Context actContext;
    private static DownloadAppManager instance;

    public static volatile  int iDownApkCount = 0;//需要下载的数量
    public static volatile  int iDownApkIndex =0;//当前下载的标示



    //需要下载的数据列表
    private List<String> downAppList =  Collections.synchronizedList(new ArrayList<String>());

    //下载服务
    public DownloadService.DownloadBinder mDownloadBinder;

    /**
     * 服务连接
     */
    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            KLog.e(LOG_TAG,"--------------------onServiceConnected-------------");
            mDownloadBinder = (DownloadService.DownloadBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            KLog.e(LOG_TAG,"--------------------onServiceDisconnected-------------");
            mDownloadBinder = null;
        }
    };
    /**
     * 初始化
     * @param context
     */
    public static DownloadAppManager initDownLoadManager(Context context){
        if(instance==null){
            synchronized(DownloadAppManager.class){
                if(instance==null){
                    actContext = context;
                    instance = new DownloadAppManager();
                }
            }
        }
        return instance;
    }

    /**
     * 构造函数
     */
    private DownloadAppManager() {
        Intent intent = new Intent(actContext, DownloadService.class);
        actContext.startService(intent);
        actContext.bindService(intent, mConnection, BIND_AUTO_CREATE);//绑定服务

    }


    /**
     * 销毁定时检测更新服务
     */
    public void destoryDownLoadService(){
        try {
            if (mConnection != null) {
                actContext.unbindService(mConnection);
            }
        }catch (Exception e){

        }
        instance = null;
    }

    /**
     * 开始下载app 服务
     */
    public void startDownLoadApp(List<String> datas){
        if(downAppList==null){
            downAppList = Collections.synchronizedList(new ArrayList<String>());
        }
        if(downAppList!=null){
            downAppList.clear();
        }
        downAppList.addAll(datas);
        //发送下载数据
        startApkDownInfo();
    }

    /**
     * 开启下载服务
     */
    private void startApkDownInfo() {
        try {
            KLog.e(LOG_TAG,"--------------------startApkDownInfo-----------d----");
            if (downAppList != null && !downAppList.isEmpty()) {
                KLog.e(LOG_TAG,"--------------------startApkDownInfo-----------downAppList is size:" + downAppList.size());
                //发送下载
                iDownApkCount = downAppList.size();
                KLog.e(LOG_TAG,"---------startApkDownInfo--------:"+iDownApkCount);
                if(mDownloadBinder!=null){
                    KLog.e(LOG_TAG, "--------------------startApkDownInfo-----------d--rrr--");
                    for (String strUrl : downAppList) {
                        KLog.e(LOG_TAG,"---------startApkDownInfo-----------mDownloadBinder----is not null--------");
                        KLog.e(LOG_TAG,"-------------startApkDownInfo------------url:"+strUrl);
                        long downloadId = mDownloadBinder.startDownload(strUrl);
                        //保存关联关系
                        MyApplication.appUrlDownIdMap.put(strUrl,downloadId);
                        KLog.e(LOG_TAG,"----------startApkDownInfo-----------------:" + downloadId);
                    }
                    //下发下载
                    Intent mDownLoadIntent = new Intent();
                    mDownLoadIntent.setAction("android.myapp.download.downloadstart");
                    actContext.sendBroadcast(mDownLoadIntent);
                }else{
                    //升级失败
                    Intent mDownLoadIntentFails = new Intent();
                    mDownLoadIntentFails.setAction("android.myapp.download.downloadfails");
                    actContext.sendBroadcast(mDownLoadIntentFails);
                    KLog.e(LOG_TAG,"---------startApkDownInfo-----------mDownloadBinder-----------is null --");
                }
            }
        }catch (Exception e){
            Intent mDownLoadIntentFails = new Intent();
            mDownLoadIntentFails.setAction("android.myapp.download.downloadexecption");
            actContext.sendBroadcast(mDownLoadIntentFails);
            KLog.e(LOG_TAG,"---------startApkDownInfo-----------mDownloadBinder------Exception------:"+e.getMessage());
        }
    }

}
